package org.database.filter.service.impl;

import java.util.List;

import org.database.filter.model.Nganh;
import org.database.filter.repository.NganhRepository;
import org.database.filter.service.NganhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class NganhServiceImpl implements NganhService {

	@Autowired
	private NganhRepository nganhRepository;

	@Override
	public Long saveOrUpdate(Nganh nganh) {
		List<Nganh> list = nganhRepository.getNganh(nganh.getTEN_NGANH());
		if (list == null || list.isEmpty()) {
			Nganh n = nganhRepository.save(nganh);
			return n.getMA_NGANH();
		}
		return list.get(0).getMA_NGANH();

	}

	@Override
	public Nganh getNganhById(Long Id) {
		return nganhRepository.findOne(Id);
	}

}
